package com.mymoney;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.mymoney.adapters.ListAdapter;
import com.mymoney.helpers.AppStatus;
import com.mymoney.helpers.ItemsHelper;
import com.mymoney.ws.asynctasks.GetPlannedItemsAsyncTask;
import com.mymoney.ws.asynctasks.RegisterDeviceAsyncTask;
import com.mymoney.ws.asynctasks.UpdateItemAsyncTask;
import com.mymoney.ws.data.ItemBean;

public class MyMoneyActivity extends BaseActivity {
	
	private  ListView planItems = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
	        
        planItems = (ListView) findViewById(R.id.planItems);

		registerDevice();

    	//showDialog("Please connect to Internet...");
    	//startActivityForResult(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS), 0);
    }
	
    public void onReadyToBuy(ItemBean item)
    {
	    showLoadingDialog("Моля изчакайте...");
	    buyItem(item);
    }
    
    public void reload()
    {
    	listPlanItems(planItems);
    }
    
    private void initListItems(ListView planItems, final ItemBean[] items)
    {
        ListAdapter adapter = new ListAdapter(this,android.R.layout.simple_expandable_list_item_1, android.R.id.text1, items);
    	planItems.setAdapter(adapter);
        
        planItems.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
             
		    ItemsHelper itemHelper = new ItemsHelper(MyMoneyActivity.this);
		    itemHelper.showItemDialog(MyMoneyActivity.this, items[position]);
			}
		});
    }
    
    private void buyItem(ItemBean item)
    {
    	UpdateItemAsyncTask updateItem = new UpdateItemAsyncTask(this, item) {
			
			@Override
			public void onFinish() {
				listPlanItems(planItems);
			}

		};
		updateItem.execute();
    }
    
    private void registerDevice(){  
    	
    	if(!AppStatus.isAccountAnnalized)
    	{
    		showLoadingDialog("Актуализиране на профила...");
    	}
    	RegisterDeviceAsyncTask register = new RegisterDeviceAsyncTask(this) {
			
			@Override
			public void onFinish() {
				setLoadingDialogText("Зареждане на продукти...");
				listPlanItems(planItems);
			}
		};
		register.execute();
    }
    
    private ItemBean[] listPlanItems(final ListView planItems)
    {
    	ItemBean[] result = null;
    	
    	final GetPlannedItemsAsyncTask plannedItems = new GetPlannedItemsAsyncTask(this) {
    		
			@Override
			public void onFinish() {
				ItemBean[] result = getListOfItems();
				
				if(result != null)
				{
					if(result.length == 0)
					{
						showToast("Нямата планирани продукти");
					}
					else
					{
						initListItems(planItems,getListOfItems());
					}
				}
				else
				{
					showToast("Моля проверете Вашата Интернет връзка");
				}
				hideLoadingDialog();
			}
		};
		
		plannedItems.execute();
		
		return result;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add(1,0,0,"Продукт").setIcon(R.drawable.ic_menu_add);
        menu.add(1,1,1,"Помощ").setIcon(R.drawable.ic_menu_info);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	int optionId = item.getItemId();
    	
    	switch (optionId) {
			case 0:
			{
		    	ItemsHelper itemsHelper = new ItemsHelper(this);
		    	itemsHelper.regisetrItemDialog();
			}break;
			
			case 1:
			{
				showToast("ПОМОЩ");
			}break;
		}

    	return true;
    }
}